﻿/*
[]
*/

Ext.namespace("tk","tk.yunos","tk.yunos.system");

/**
 * 依赖js：ext3.2，jquery1.7.1
 * 依赖css: ../browser.css
 * 父类：Ext.Panel
 * 特殊配置项: 
		url: 默认打开的链接
 * 		hideToolbar: 是否隐藏工具栏，包括导航条和地址栏
 * 		home: 主页
**/

tk.yunos.system.Browser = function(fileDescriptor, config){
	var panel = new tk.yunos.system.Browser.Panel(fileDescriptor, config);
	if(panel == null){
		return;
	}
	var winParams = {
		title: tk.yunos.system.Browser.appname + '[' + fileDescriptor.address + ']',
		icon: fileDescriptor.icon,
		layout: 'fit',
		items: [
		    panel
		]
	};
	tk.yunos.system.GLOBAL_WINDOWMGR.newWindow(winParams).show();
};

tk.yunos.system.Browser.Panel = function(fileDescriptor, config){
	var config = config || {};
	var browser = this;
	config.hideToolbar = config.hideToolbar || false;
	config.url = config.url || "about:blank";
	
	var fileConfig = null;
	if(fileDescriptor){
		if(fileDescriptor.url && fileDescriptor.url != ''){
			//如果在文件描述符中配置了url
			config.url = fileDescriptor.url;
		}else{
			try{
				tk.yunos.filesystem.getFileContent(fileDescriptor.address, function(success, fileContent){
					if(success){
						try{
							fileConfig = Ext.decode(fileContent);
							Ext.apply(config, fileConfig);
						}catch(e){
						}
					}
				}, true);
			}catch(e){}
		}
	}
	if(fileConfig != null && fileConfig.newwindow){
		tk.yunos.system.finish();
		window.open(fileConfig.url);
		return null;
	}
	config.home = config.home || config.url;
	config.id = config.id || Ext.id();
	
	var gotoURL = function(url){
		browser.getWindow().wait();
		if(url.indexOf('://') < 0){
			url = tk.yunos.system.Browser.addProtocol(url);
		}
		document.getElementById(config.id + '-frame').src = encodeURI(url);
	};
	
	var toolPanel = new Ext.Panel({
		hidden: config.hideToolbar,
		region: 'north',
		layout: 'column',
		frame: true,
		height: 40,
		defaultType: 'button',
		defaults: {
			width: 30
		},
		items: [
			{
				iconCls: 'back',
				tooltip: '后退',
				handler: function(){
					browser.getWindow().wait();
					history.back();
				}
			},{
				iconCls: 'forward',
				tooltip: '前进',
				handler: function(){
					browser.getWindow().wait();
					history.forward();
				}
			},{
				iconCls: 'home',
				tooltip: '主页',
				handler: function(){
					gotoURL(config.home);
				}
			},{
				iconCls: 'add',
				tooltip: '新窗口',
				handler: function(){
					tk.yunos.system.GLOBAL_WINDOWMGR.newWindow({
						title: '浏览器',
						iconCls: 'browser',
						width: 600,
						height: 500,
						layout: 'fit',
						items: [
							new tk.yunos.system.Browser({
							})
						]
					}).show();
				}
			},{
				xtype: 'label',
				width: 50,
				style: 'padding-left:10px;font-size:14px;font-weight:bold;',
				text: '地址:'
			},{
				columnWidth: 0.98,
				xtype: 'textfield',
				id: config.id + 'address',
				value: config.url,
				style: 'margin-right:2px',
				height: 24,
				enableKeyEvents: true,
				listeners: {
					'keypress': function(tf, e){
						if(e.browserEvent.keyCode == e.ENTER){
							gotoURL(tf.getValue());
						}
					}
				}
			},{
				iconCls: 'goto',
				width: 50,
				handler: function(){
					gotoURL(Ext.getCmp(config.id + 'address').getValue());
				}
			}
		]
	});
	
	var contentArea = new Ext.Panel({
		region: 'center',
		border: false,
		activeTab: 0,
		html: '<iframe id="' + config.id + '-frame" name="' + config.id + '-frame" ' +
			'style="width:100%;height:100%;overflow:auto;" frameborder="0" ' +
			'src="' + encodeURI(config.url) + '" onload="tk.yunos.system.Browser.contentLoad(\'' + config.id + '\')"/>',
		listeners: {
			'beforedestroy': function(){
				document.getElementById(config.id + '-frame').src = '';
			}
		}
	});
	
	config.items = [toolPanel,contentArea];
	
	Ext.apply(this, config);
	tk.yunos.system.Browser.Panel.superclass.constructor.call(this, config);
	
	this.on('render', function(){browser.getWindow().wait();});
};

Ext.extend(tk.yunos.system.Browser.Panel, Ext.Panel, {
	border: false,
	//frame: true,
	layout: 'border'
});

tk.yunos.system.Browser.contentLoad = function(id){
	var src = document.getElementById(id + '-frame').src;
	win = Ext.getCmp(id).ownerCt;
	var domain = tk.yunos.system.Browser.getMainDomain(src);
	if(win.title.indexOf('-' + domain) < 0){
		if(!new RegExp("^.*\\[.*\\].*$").test(win.title)){
			win.setTitle(win.title + ' [' + domain + ']');
		}
	}
	win.finish();
};

//顶级域名，国际域名在前，国家域名在后
tk.yunos.system.Browser.topDomains = ['com', 'net', 'info', 'org', 'gov', 'mobi', 'me', 'co', 'asia', 'biz', 'so', 'tv', 'tel',
	'cn', 'hk', 'us', 'uk', 'jp', 'cc'];
	
tk.yunos.system.Browser.getMainDomain = function(wholeURL){
	var fail = '呃，这是啥网站？';
	var tmp = wholeURL.split('/');
	tmp = tmp[2];
	if(tmp){
		tmp += '/';
		var topDomains = tk.yunos.system.Browser.topDomains;
		for(var i = 0; i < topDomains.length; i++){
			var exps = ['.' + topDomains[i] + '/', '.' + topDomains[i] + '.'];
			for(var j = 0; j < exps.length; j++){
				if(tmp.indexOf(exps[j]) >= 0){
					var sep = tmp.lastIndexOf(exps[j]);
					var result = tmp.substring(0, sep);
					result = result.split('.');
					result = result[result.length - 1];
					if(result){
						return result;
					}
				}
			}	
		}
	}
	return fail;
};

tk.yunos.system.Browser.addProtocol = function(url){
	return 'http://' + url;
};

tk.yunos.system.Browser.appname = '网页浏览器';
